Attribute VB_Name = "modSafeArray"
'***************************************************************
' (c) Copyright 2000 Matthew J. Curland
'
' This file is from the CD-ROM accompanying the book:
' Advanced Visual Basic 6: Power Techniques for Everyday Programs
'   Author: Matthew Curland
'   Published by: Addison-Wesley, July 2000
'   ISBN: 0-201-70712-8
'   http://www.PowerVB.com
'
' You are entitled to license free distribution of any application
'   that uses this file if you own a copy of the book, or if you
'   have obtained the file from a source approved by the author. You
'   may redistribute this file only with express written permission
'   of the author.
'
' This file depends on:
'   References:
'     VBoostTypes6.olb (VBoost Object Types (6.0))
'   Files:
'     None
'   Minimal VBoost conditionals:
'     None
'   Conditional Compilation Values:
'     None
'
' This file is discussed in Chapter 2.
'***************************************************************
Option Explicit

Public Sub ShareMemoryViaArray(ByVal ArrayPtr As Long, ByVal MemPtr As Long, SA1D As SafeArray1D, Optional ByVal ElemByteLen As Long)
    With SA1D
        'cbElements is optional because this is a 1 element array,
        'so cbElements is not needed to walk the array.  If Erase
        'is called on an array with .cbElements = 0, VB will still
        'free all pointer types, but non-pointer types will not get
        'zeroed out.  Note that the compiler calculates the length
        'of a structure at compile time, so LenB(MyStruct(0)) is
        'valid regardless of whether or not MyStruct is actually allocated.
        .cbElements = ElemByteLen
        .cDims = 1
        'This means that if the
        'array goes out of scope, then the pointed
        'to memory will be cleaned, but no attempt
        'will be made to free the array pointer
        'or descriptor.
        .fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
        .pvData = MemPtr
        .cElements = 1
    End With
    CopyMemory ByVal ArrayPtr, VarPtr(SA1D), 4
End Sub

Public Sub UnshareMemory(ByVal ArrayPtr As Long)
    ZeroMemory ByVal ArrayPtr, 4
End Sub
